package com.xiecong.service.impl;

import com.xiecong.mapper.DeptMapper;
import com.xiecong.mapper.EmpMapper;
import com.xiecong.pojo.Dept;
import com.xiecong.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    /**
     * 查询部门列表信息
     *
     * @return
     */
    @Override
    public List<Dept> findAll() {
        List<Dept> deptList = deptMapper.findAll();
        return deptList;
    }

    /**
     * 根据id删除部门信息
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        List<Integer> allDeptpId = empMapper.findAllDeptpId();
        if (allDeptpId.contains(id)) {
            throw new RuntimeException("对不起，当前部门下有员工，不能直接删除！");
        } else {
            deptMapper.deleteById(id);
        }
    }

    /**
     * 新增部门信息
     *
     * @param dept
     */
    @Override
    public void add(Dept dept) {
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateTime(now);
        dept.setUpdateTime(now);
        deptMapper.insert(dept);
    }

    /**
     * 根据id查询部门信息
     *
     * @param id
     * @return
     */
    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMapper.getById(id);
        return dept;
    }

    /**
     * 更新部门信息
     *
     * @param dept
     */
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
